<%@ page contentType="text/html;charset=UTF-8"%>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<title>物件剩余查询</title>
<meta name="decorator" content="default" />
<link rel="stylesheet" type="text/css"
	href="${ctxStatic}/easyui/themes/default/easyui.css">
<link rel="stylesheet" type="text/css"
	href="${ctxStatic}/easyui/themes/icon.css">
<link rel="stylesheet" type="text/css"
	href="${ctxStatic}/easyui/demo/demo.css">
<script type="text/javascript" src="${ctxStatic}/easyui/jquery.min.js"></script>
<script type="text/javascript"
	src="${ctxStatic}/easyui/jquery.easyui.min.js"></script>
</head>
<body>
	<input type="hidden" value="${par}" id="inputWellNo">
	<div style="margin-bottom: 5px;">
		<!-- 物件：<input class="easyui-combotree" data-options="" style="width: 300px;" id="cc" /> -->
		物件：<select id="datag" class="easyui-combogrid" style="width:30%" data-options="
				panelWidth: 500,
				panelMinWidth: '50%',
				idField: 'gid',
				textField: 'gname',
				method: 'get',
				mode : 'remote',
				columns: [[
					{field:'gid',title:'物件编号',width:60},
					{field:'gname',title:'物件名称',width:150},
					{field:'series',title:'物件系列',width:40,align:'left'}
				]],
				onSelect:function(recordidex,rowData){
                },
                onLoadSuccess:function(data){
					var value=$('#datag').combogrid('getValue');
					if(1==data.rows.length&&data.rows[0].gid==value){
						value=' '+value;
					}
					$('#datag').combogrid('setValue', value);
					
				},
               keyHandler:{ 
                   query:function(q){
	                   	<!-- if(q.length>=9){ -->
                   		<!-- q=q.trim(); -->
                   		quickDimQuery(q);
	                   	<!-- } -->
                   }
               },
				fitColumns: true
			">
			</select>
			<a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-save'" onclick="sub();">提交</a>
	</div>
	<br>
	<table id="dg" title="物件" class="easyui-datagrid"
		style="width: 100%; height: auto" data-options="
				rownumbers:true,
				singleSelect:true,
				autoRowHeight:false,
				pagination:true,
				showFooter: true">
		<thead>
			<tr>
				<th data-options="field:'code',width:150,align:'center'">物品编号</th>
				<th data-options="field:'name',width:300,align:'center'">物品名称</th>
				<th data-options="field:'amount',width:100,align:'center'">目前剩余</th>
				<th data-options="field:'repository',width:100,align:'center'">库位</th>
			</tr>
		</thead>
	</table>
	
	<div id="dlg" class="easyui-dialog"  style="width:400px;height:200px;padding:10px">
			
			<span>物料编号：</span><input id="goodsId"  class='easyui-textbox'  style='width:30%;height:22px'/>
			<div></div><br>
			<span>物料名称：</span><input id="goodsName"  class='easyui-textbox'  style='width:60%;height:22px'/><br>
	</div>
	

	<script type="text/javascript">
		var initData='${typeId}';
		function sub(){
			/* var cc=$("#cc").combotree('getValue'); */
			var cc=$("#datag").combogrid('getValue');
			var url = "${ctx}/erp/goodsType/ajQueryResidue?WellNo="+cc+"&page=1&pageSize=10";
            $('#dg').datagrid('options').url = url;
            $('#dg').datagrid('reload');
//            Query();
			return;
		}
		$(function(){
			//伪分页
			/* var initData=${list};
			$('#dg').datagrid({
				data : getData(initData)
			}).datagrid({loadFilter:pagerFilter}); */
			//真分页
			Query();
			InitPagenation(); 
			$("#dlg").dialog('close');
		})
		function quickDimQuery(q){
			var url = 'dimQueryLinkGoods';
			$("#datag").combogrid('grid').datagrid('options').url= url+"?key="+q+"&cssb="+initData;
            $("#datag").combogrid('grid').datagrid('load');
            $("#datag").combogrid('setValue', q);
		}
		//初始化分页
        function InitPagenation() {
            //设置分页控件 
            var p = $('#dg').datagrid("getPager");
            $(p).pagination({
                pageSize: 10, //每页显示的记录条数，默认为10 
                pageList: [10, 20, 30], //可以设置每页记录条数的列表 
                onSelectPage: function (pageNumber, pageSize) {
                    var inputValue =  $("#datag").combogrid('getValue');
                    var wellNo = null;
                    if(inputValue!=null&&inputValue!=''){
                        wellNo = inputValue;
					}else{
                        wellNo = $('#inputWellNo').val();
					}
                	var url = "${ctx}/erp/goodsType/ajQueryResidue?WellNo="+wellNo+"&page="+pageNumber+"&pageSize="+pageSize;
                	//TODO 增加条件查询参数
                    $('#dg').datagrid('options').url = url;
                    $('#dg').datagrid('reload'); //重新加载表格
                },
                //按钮
                buttons:[{
                	iconCls:'icon-remove',//删除
                	handler:function(){
                		var row = $('#dg').datagrid('getSelected');
                		var wellNo2 = $('#inputWellNo').val();//
                		if(null!=row){
                			$.messager.confirm('删除', '确定删除?', function(r){
								if (r){
									window.location.href="${ctx}/erp/goodsType/deleteGoods?goodsCode="+row.code+"&wellNo="+wellNo2; 
								}
							});
                		}
                	}
                },
                {
                	iconCls:'icon-add',//新增
                	handler:function(){
                		var wellNo2 = $('#inputWellNo').val();//
                		var split=wellNo2.split("-");
                		var wellNo=split[0];
                		$("#dlg").dialog({
                			onOpen:function(){
								//打开时默认设置编号开头为父编号
								$("#goodsId").textbox('setValue',wellNo);
							},
                			buttons:[{
									text:'确定',
									iconCls:'icon-ok',
	                				handler:function(){
	                					//异步校验编号是否重复
	                					var goodsId=$("#goodsId").textbox('getValue');
	                					var goodsName=$("#goodsName").textbox('getValue');
	                					$.ajax({
										    url: '${ctx}/erp/goodsType/saveGoods',         
										    data: {code:goodsId,name:goodsName,type:wellNo},
										    dataType: "json",
										    type: "POST",
										    success: function (data) {
										    	if('1'==data){
										    		$.messager.alert('新增','新增完成!','info',function(){
										    			window.location.href="${ctx}/erp/goodsType/queryResidue?typeId="+wellNo2; 
										    		});
										    	}else{
										    		$.messager.alert('新增','物料编号或名称已经存在','error');
										    	}
										    }
										});
	                				}
								},
                				{
                					text:'取消',
    								handler:function(){
    									$("#dlg").dialog('close');
    								}
                				}
                			],
                			title:"新增"
                		});
                	}
                },
                {
                	iconCls:'icon-edit',//修改
                	handler:function(){
                		var row = $('#dg').datagrid('getSelected');
                		var wellNo2 = $('#inputWellNo').val();//
                		if(null!=row){
                			$("#dlg").dialog({
                				onOpen:function(){
                        			$("#goodsId").textbox('setValue',row.code).textbox('textbox').attr('readonly',true);
                        			$("#goodsName").textbox('setValue',row.name);
                        		},
                        		buttons:[{
                        			text:'修改',
									iconCls:'icon-ok',
	                				handler:function(){
	                					//异步校验编号是否重复
	                					var goodsId=$("#goodsId").textbox('getValue');
	                					var goodsName=$("#goodsName").textbox('getValue');
	                					$.ajax({
										    url: '${ctx}/erp/goodsType/updateGoods',         
										    data: {code:goodsId,name:goodsName},
										    dataType: "json",
										    type: "POST",
										    success: function (data) {
										    	if('1'==data){
										    		$.messager.alert('编辑','编辑完成!','info',function(){
										    			window.location.href="${ctx}/erp/goodsType/queryResidue?typeId="+wellNo2;
										    		});
										    	}else{
										    		$.messager.alert('编辑','物料名称已经存在','error');
										    	}
										    }
										});
	                				}
                        		},
                				{
                					text:'取消',
    								handler:function(){
    									$("#dlg").dialog('close');
    								}
                				}]
                			});
                		}
                		
                	}
                }]
            });
        }
        function Query() {
            var wellNo = $('#inputWellNo').val(); //条件参数
           /*  var opts = $('#dg').datagrid('options');
            var page = opts.pageNumber;//获取页码
            var pageSize = opts.pageSize;//获取每页多少记录 */
            var url = "${ctx}/erp/goodsType/ajQueryResidue?WellNo="+wellNo+"&page=1&pageSize=10";
            $('#dg').datagrid('options').url = url; //设置表格数据的来源URL
            $('#dg').datagrid({onLoadSuccess : function(data){
            }});
            $('#dg').datagrid('reload'); //重新加载表格
        }
		
		function getData(initData) {
			var rows=[];
			for(var i=0;i<initData.length;i++){
				rows.push({
					gCode :initData[i].code,
					gName :initData[i].name,
					residue : initData[i].amount
				});
			}
			return rows;
		}
		function pagerFilter(data){
			if (typeof data.length == 'number' && typeof data.splice == 'function'){	// is array
				data = {
					total: data.length,
					rows: data
				}
			}
			var dg = $(this);
			var opts = dg.datagrid('options');
			var pager = dg.datagrid('getPager');
			pager.pagination({
				onSelectPage:function(pageNum, pageSize){
					opts.pageNumber = pageNum;
					opts.pageSize = pageSize;
					pager.pagination('refresh',{
						pageNumber:pageNum,
						pageSize:pageSize
					});
					dg.datagrid('loadData',data);
				}
			});
			if (!data.originalRows){
				data.originalRows = (data.rows);
			}
			var start = (opts.pageNumber-1)*parseInt(opts.pageSize);
			var end = start + parseInt(opts.pageSize);
			data.rows = (data.originalRows.slice(start, end));
			return data;
		}
	</script>
</body>
</html>